#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Create by zhang
# Create on 2022/6/22 14:43
from domain.report.common.period_enum import PeriodEnum
from domain.report.common.str_util import del_html_element
from domain.report.dto.topten_dto import TopTenDTO, ShareholdingInfoDTO
from typing import List


def xbrl2dtos(data, stock_code, period:PeriodEnum) -> List[TopTenDTO]:
    columns = data["columns"]
    rows = data["rows"]
    dto_list = list()
    for i in range(len(columns)):
        dto = TopTenDTO()
        dto.stock_code:str = stock_code
        dto.stock_id = int(stock_code.split(".")[0])
        dto.period = period
        dto.year = int(del_html_element(data["columns"][i]["title"]))
        for j in range(len(data["rows"])):
            val = del_html_element(rows[j][del_html_element(columns[i]["field"])])
            val = val.replace("-", "") if val and "-" in val else val
            val_arr = val.split("</br>")
            holdinfo_dto = None
            if val_arr and len(val_arr) == 3:
                holdinfo_dto = ShareholdingInfoDTO()
                holdinfo_dto.shareholder = val_arr[0].split("：")[1]
                holdinfo_dto.amount = val_arr[1].split("：")[1]
                holdinfo_dto.ratio = val_arr[2].split("：")[1]
            dto.__setattr__(f"v{j}", holdinfo_dto)
        dto_list.append(dto)
    return dto_list
